/**
 *  Copyright (C) 2006 zhangbo (freeeob@gmail.com)
 *
 *  This product is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 * 
 *  This product is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 *
 *  author:zhangbo
 *  Email:jsjava@gmail.com
 */
 
function ChinaConvertUtils(){
	this.jsjava_class="jsorg.eob.convert.country.cn.ChinaConvertUtils";
}

ChinaConvertUtils.NUMBER_LOWER_CHINESE=["\u96f6","\u4e00","\u4e8c","\u4e09","\u56db","\u4e94","\u516d","\u4e03","\u516b","\u4e5d"];
ChinaConvertUtils.NUMBER_UPPER_CHINESE=["\u96f6","\u58f9","\u8d30","\u53c1","\u8086","\u4f0d","\u9646","\u67d2","\u634c","\u7396"];
ChinaConvertUtils.SHI="\u5341";
ChinaConvertUtils.BAI="\u767e";
ChinaConvertUtils.QIAN="\u5343";
ChinaConvertUtils.WAN="\u4e07";
ChinaConvertUtils.YI="\u4ebf";
ChinaConvertUtils.ZHAO="\u5146";
ChinaConvertUtils.DIAN="\u70b9";
ChinaConvertUtils.FENZHI="\u5206\u4e4b";
ChinaConvertUtils.FU="\u8d1f";

/**
 * Convert the number to chinese words
 * @param num
 * @param isLower
 */
ChinaConvertUtils.numberToChinese=function(num,isLower){
	var narr=ChinaConvertUtils.NUMBER_UPPER_CHINESE;
	if(isLower!=undefined&&isLower==true){
		narr=ChinaConvertUtils.NUMBER_LOWER_CHINESE;
	}
	function c2c(str){
		if(str==undefined){
			return "";
		}
		var s="";
		for(var i=0;i<str.length;i++){
			s+=narr[parseInt(str.charAt(i))];
		}
		return s;
	}
	if(num==undefined){
		throw new IllegalArgumentException(IllegalArgumentException.ERROR,"Invalid arguments");
	}	
	var isZhengShu=isFenShu=isBaiFenShu=isQianFenShu=isXiaoShu=false;
	var fsPos=num.indexOf("/");
	var bfsPos=num.indexOf("%");
	var xsPos=num.indexOf(".");
	if(fsPos!==-1){
		if(fsPos==0||fsPos==num.length-1){
			throw new IllegalArgumentException(IllegalArgumentException.ERROR,"Invalid arguments");
		}
		isFenShu=true;
	}else if(bfsPos!=-1){
		if(bfsPos!=num.length-1){
			throw new IllegalArgumentException(IllegalArgumentException.ERROR,"Invalid arguments");
		}
		isBaiFenShu=true;
	}else if(xsPos!=-1){
		isXiaoShu=true;
	}else{
		isZhengShu=true;
	}	
	var isFuShu=false;
	if(num.indexOf("-")==0){
		isFuShu=true;
	}
	var str="";
	if(isFuShu){
		str+=ChinaConvertUtils.FU;
	}
	if(isFenShu){
		var bstr=num.substring(0,fsPos);
		var astr=num.substring(fsPos+1);
		str+=c2c(astr)+ChinaConvertUtils.FENZHI+c2c(bstr);
		return str;
	}
	if(isBaiFenShu){
		var bstr=num.substring(0,bfsPos);
		str+=ChinaConvertUtils.BAI+ChinaConvertUtils.FENZHI+c2c(bstr);
		return str;
	}
	if(isXiaoShu){
		var bstr=num.substring(0,xsPos);
		if(bstr==""){
			bstr="0";
		}
		var astr=num.substring(xsPos+1);
		str+=c2c(bstr)+ChinaConvertUtils.DIAN+c2c(astr);
		return str;
	}
	str+=c2c(num);
	return str;
};

/**
 * Convert the number to chinese lowercase words
 * @param num
 */
ChinaConvertUtils.numberToLowerChinese=function(num){
	return ChinaConvertUtils.numberToChinese(num,true);
};

/**
 * Convert the number to chinese uppercase words
 * @param num
 */
ChinaConvertUtils.numberToUpperChinese=function(num){
	return ChinaConvertUtils.numberToChinese(num,false);
};